/*
 * @Author: wang cong wangc9832@163.com
 * @Date: 2023-05-09 10:28:05
 * @LastEditors: wang cong wangc9832@163.com
 * @LastEditTime: 2023-12-01 09:09:03
 * @FilePath: /ams_kt_front/src/components/Table/src/hooks/useTableHeader.ts
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import type { ComputedRef, Ref, Slots } from 'vue';
import type {
  BasicTableProps,
  InnerHandlers,
  TableRowSelection,
} from '../types/table';
import { unref, computed, h } from 'vue';
import TableHeader from '../components/TableHeader.vue';
import { isString } from '@/utils/is';
import { getSlot } from '@/utils/helper/tsxHelper';
export function useTableHeader(
  propsRef: ComputedRef<BasicTableProps>,
  formRef: Ref<ComponentRef>,
  getRowSelection: () => TableRowSelection<any>,
  slots: Slots,
  handlers: InnerHandlers
) {
  const getHeaderProps = computed((): Recordable => {
    const {
      title,
      // 用于高级查询修改查询参数
      beforeFetch,
      // 新增传参
      columns,
      options,
      showHSearch,
      formConfig,
      showTableSetting,
      titleHelpMessage,
      tableSetting,
      tableKey,
      loadClear,
    } = unref(propsRef);
    const hideTitle =
      !slots.tableTitle && !title && !slots.toolbar && !showTableSetting;
    if (hideTitle && !isString(title)) {
      return {};
    }

    return {
      title: hideTitle
        ? null
        : () =>
            h(
              TableHeader,
              {
                loadClear,
                title,
                titleHelpMessage,
                columns,
                options,
                getRowSelection,
                searchFormSchema: formConfig?.schemas,
                showTableSetting,
                // 用于获取查询参数
                formRef,
                showHSearch,
                tableSetting: { ...tableSetting, tableKey },
                onAdvancedSearch: handlers.onAdvancedSearch,
                onAdvancedSetDefault: handlers.onAdvancedSetDefault,
                onColumnsChange: handlers.onColumnsChange,
                onRemoveSuccess: handlers.onRemoveSuccess,
              } as Recordable,
              {
                ...(slots.toolbar
                  ? {
                      toolbar: () => getSlot(slots, 'toolbar'),
                    }
                  : {}),
                ...(slots.tableTitle
                  ? {
                      tableTitle: () => getSlot(slots, 'tableTitle'),
                    }
                  : {}),
                ...(slots.headerTop
                  ? {
                      headerTop: () => getSlot(slots, 'headerTop'),
                    }
                  : {}),
              }
            ),
    };
  });
  return { getHeaderProps };
}
